def key = session.user.id + '-weekTranscations'
if(!memcache.contains(key)) {
	def now =  new Date() - Calendar.getInstance().get(Calendar.DAY_OF_WEEK)
	now.clearTime()
	def tentities = datastore.execute {
		from TranscationE
		sort desc by createdTime
		where userId == session.user.id and createdTime > now
	}

	def transcationTags = [:]
	def tagTotal = [:]
	def weekTotal = 0.0
	tentities.each { transcation ->
		def ttentities = datastore.execute {
			from TranscationTagE
			sort asc by name
			where transcationId == transcation.key.id and userId == session.user.id
		}
		def tags = new StringBuilder()
		ttentities.each { tt ->
			tags += "<a class=\"table-1-link\" href=\"transcationViewForTag.groovy?transcationTag=$tt.name\">#$tt.name</a> "
			if(tagTotal[tt.name] == null) {
				tagTotal[tt.name] = 0
			}
			tagTotal[tt.name] += transcation.amount
		}
		transcationTags[transcation.key.id] = tags
		weekTotal += transcation.amount
	}

	key = session.user.id + '-weekTranscations'
	memcache[key] = tentities
	key = session.user.id + '-weekTagTotal'
	memcache[key] = tagTotal
	key = session.user.id + '-weekTotal'
	memcache[key] = String.format("%.2f", weekTotal)
	key = session.user.id + '-weekTranscationTags'
	memcache[key] = transcationTags
}
request.transcationTags = memcache[session.user.id + '-weekTranscationTags']
request.tagTotal = memcache[session.user.id + '-weekTagTotal']
request.total = memcache[session.user.id + '-weekTotal']
request.transcations = memcache[session.user.id + '-weekTranscations']

forward 'transcationViewForWeek.gsp'